local schedule = require("framework.scheduler");

local StartLayer = class("StartLayer", function()
	local layer = display.newSprite("bg_blank.png");
	layer:pos(display.cx,display.cy);
	layer:setContentSize(cc.size(display.width,display.height));
	layer:setTouchEnabled(true);
	layer:setVisible(false);
	return layer;
end);

StartLayer.delegate = {
	onStartPlay = "onStartPlay";	
};

function StartLayer:ctor()
	self:initUI();
end

function StartLayer:initUI()
	local param = {
        UILabelType = 1,
        text  = "Ready?",
        font = "font/UIFont.fnt", 
        align = cc.ui.TEXT_ALIGN_LEFT,
    };
	self.m_tips = cc.ui.UILabel.new(param)
	:align(display.CENTER, display.cx, display.cy)
    :addTo(self);
end

function StartLayer:setDelegate(delegate)
	self.m_delegate = delegate;
end 

function StartLayer:show()
	self:startScaleAnim()
	self:updateTips("Ready?");
	self:startTimer();
	self:setVisible(true);
end

function StartLayer:startScaleAnim()
	local scaleTo1 = cc.ScaleTo:create(0.5,1.2,1.2); 
	local scaleTo2 = cc.ScaleTo:create(0.5,1.0,1.0);
	-- cc.RepeatForever:create(scaleTo1);
	self.m_tips:runAction(cc.RepeatForever:create(cc.Sequence:create(scaleTo1,scaleTo2)));
end  

function StartLayer:updateTips(tips)
	self.m_tips:setString(tostring(tips));
end 

function StartLayer:startTimer()	
	self.m_count = 4;
	self.m_schedule_handler = schedule.scheduleGlobal(handler(self,self.onTimeEvent),1);
end  

function StartLayer:onTimeEvent()
	self.m_count = self.m_count - 1;
	if self.m_count >= 1 then 
		self:updateTips(self.m_count);
	else 
		schedule.unscheduleGlobal(self.m_schedule_handler);
		self.m_schedule_handler = nil;
		self:setVisible(false);
		self:executeDelegate(StartLayer.delegate.onStartPlay);
	end 
end

function StartLayer:executeDelegate(func,...)
	if self.m_delegate and self.m_delegate[func] then 
		self.m_delegate[func](self.m_delegate,...);
	end 
end 

return StartLayer;